package org.kfive.nova;

import java.io.PrintStream;

import sun.jvm.hotspot.code.CodeBlob;
import sun.jvm.hotspot.oops.Method;
import sun.jvm.hotspot.oops.Symbol;
import sun.jvm.hotspot.runtime.Frame;
import sun.jvm.hotspot.runtime.JavaThread;
import sun.jvm.hotspot.runtime.VM;

/**
 * @program: nova
 * @author: zhanwu.lzw
 * @create: 2020-08-16 13:07
 **/
public class JvmTest {

    public static void main(String[] args) {
        JvmTest test = new JvmTest();
        test.dupmStack(VM.getVM().getThreads().first(),System.out);
    }

    public void dupmStack(JavaThread cur, PrintStream tty){
        Frame f = cur.getCurrentFrameGuess();
        while (f!=null){
            tty.println("帧：PC = "+f.getPC()+",SP = "+f.getSP()+", FP = "+f.getFP());
            Method curMethod = f.getInterpreterFrameMethod();
            if(curMethod!=null){
                Symbol methodSymbol = curMethod.getName();
                tty.println("方法名："+methodSymbol.asString());
            }
            if(VM.getVM().getCodeCache().contains(f.getPC())){
                CodeBlob cb = VM.getVM().getCodeCache().findBlob(f.getPC());
                tty.print("CodeBlob:"+cb.toString());
                tty.print(", codeBegin = "+cb.codeBegin());
                tty.print(", codeEnd = "+cb.codeEnd());
                tty.println();
            }
        }
    }
}
